package com.tech.connect.activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.Html;
import android.text.Spanned;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.TextView;

import com.gyf.barlibrary.ImmersionBar;
import com.ksy.common.login.QQInfo;
import com.ksy.common.login.QQPlatform;
import com.ksy.common.login.WeChatInfo;
import com.ksy.common.login.WeChatPlatform;
import com.ksy.common.utils.Constants;
import com.ksy.common.utils.LogTool;
import com.tech.connect.R;
import com.tech.connect.api.BaseEntityOb;
import com.tech.connect.api.CurrentInfo;
import com.tech.connect.api.HttpUtils;
import com.tech.connect.api.UserInfo;

import java.util.Map;
import java.util.regex.Pattern;

public class LoginActivity extends BaseActivity {

    private WeChatPlatform weChatPlatform;
    private QQPlatform qqPlatform;
    private EditText etPhone;
    private EditText etPassword;
    private CheckBox checkboxPassword;
    private CheckBox checkboxServer;
    private TextView tvServer;
    private TextView tvForgot;
    private TextView tvRegister;
    private TextView loginWeChat;
    private TextView loginQQ;
    private Button loginPhone;

    @Override
    protected void initImmersionBar(ImmersionBar mImmersionBar) {
        mImmersionBar
                .fitsSystemWindows(false)
                .keyboardEnable(false)
                .init();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getHeadBar().setTitle("登录");
        setContentView(R.layout.activity_login);
        initView();
//        findViewById(R.id.loginPhone).performClick();
    }

    private void initView() {
        etPhone = findViewById(R.id.etPhone);
        etPassword = findViewById(R.id.etPassword);
        etPhone.setText("13233919756");
        etPassword.setText("111111");
        checkboxPassword = findViewById(R.id.checkboxPassword);
        checkboxPassword.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    etPassword.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
                } else {
                    etPassword.setTransformationMethod(PasswordTransformationMethod.getInstance());
                }
                etPassword.setSelection(etPassword.getText().length());
            }
        });
        checkboxServer = findViewById(R.id.checkboxServer);
        checkboxServer.setChecked(true);
        tvServer = findViewById(R.id.tvServer);
        tvServer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
            }
        });
        Spanned server = Html.fromHtml("<u><font color='#ec5f5a'>服务条款</font></u>");
        tvServer.setText(server);
        loginPhone = findViewById(R.id.loginPhone);
        tvForgot = findViewById(R.id.tvForgot);
        tvForgot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                //
                jump2Activity(ForgotPasswordActivity.class);
            }
        });
        Spanned forgot = Html.fromHtml("<u><font color='#989898'>忘记密码?</font></u>");
        tvForgot.setText(forgot);
        tvRegister = findViewById(R.id.tvRegister);
        tvRegister.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                jump2Activity(RegisterActivity.class, 996);
            }
        });
        loginWeChat = findViewById(R.id.loginWeChat);
        loginQQ = findViewById(R.id.loginQQ);
        loginQQ.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                qqPlatform = new QQPlatform(activity);
                if (!qqPlatform.isCanLogin()) {
                    showToast("请先安装QQ");
                    return;
                }
                qqPlatform.loginTo(LoginActivity.this, new QQPlatform.Listener() {
                    @Override
                    public void onSuccess(QQInfo info) {
                        LogTool.e("");
                    }

                    @Override
                    public void onError(String msg) {
                        showToast(msg);
                    }

                    @Override
                    public void onCancel() {

                    }
                });
            }
        });
        loginWeChat.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                weChatPlatform = new WeChatPlatform(activity);
                if (!weChatPlatform.isCanLogin()) {
                    showToast("请先安装微信");
                    return;
                }
                weChatPlatform.loginTo(LoginActivity.this, new WeChatPlatform.Listener() {
                    @Override
                    public void onSuccess(WeChatInfo info) {
                        LogTool.e("");
                    }

                    @Override
                    public void onError(String msg) {
                        showToast(msg);
                    }

                    @Override
                    public void onCancel() {
                        LogTool.e("weChatPlatform     onCancel ");
                    }
                });
            }
        });
        findViewById(R.id.loginPhone).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String phone = etPhone.getText().toString().trim();
                String password = etPassword.getText().toString().trim();
                if (isEmpty(phone)) {
                    showToast("请输入手机号");
                    return;
                }
                if (!Pattern.matches(Constants.regularPhone, phone)) {
                    showToast("请输入正确的手机号码");
                    return;
                }
                if (isEmpty(password)) {
                    showToast("请输入密码");
                    return;
                }
                if (!checkboxServer.isChecked()) {
                    showToast("请阅读并同意服务条款");
                    return;
                }

                Map<String, Object> map = HttpUtils.getMap();
                map.put("loginName", phone);
                map.put("password", password);
                HttpUtils.login(map, new BaseEntityOb<Map<String, String>>() {
                    @Override
                    public void onStart() {
                        super.onStart();
                        showDialog();
                    }

                    @Override
                    public void onDataDeal(boolean isSuccess, Map<String, String> data, String msg) {
                        if (isSuccess) {
                            String userId = data.get("userId");
                            String token = data.get("token");
                            CurrentInfo.getUserInfo().token = token;
                            getUserInfo();
                        } else {
                            showToast(msg);
                            hideDialog();
                        }
                    }
                });
            }
        });
    }

    private void getUserInfo() {
        HttpUtils.getUserInfo(new BaseEntityOb<UserInfo>() {
            @Override
            public void onDataDeal(boolean isSuccess, UserInfo data, String msg) {
                UserInfo.getUserInfo().setDataInfoByLogIn(data);
                setResult(RESULT_OK);
                jump2Activity(MainActivity.class);
                destroyActivity();
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (weChatPlatform != null) {
            weChatPlatform.onActivityResult(requestCode, resultCode, data);
        }
        if (qqPlatform != null) {
            qqPlatform.onActivityResult(requestCode, resultCode, data);
        }
        if (requestCode == 996 && resultCode == RESULT_OK) {
            if (data != null) {
                String phone = data.getStringExtra("phone");
                etPhone.setText(phone);
                etPassword.setText("");
            }
        }

    }
}